﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace regExpTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                txtLog.Text = ofd.FileName;
                txtSource.Text = (new System.IO.StreamReader(txtLog.Text)).ReadToEnd();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            doTest();

        }
        private void doTest()
        {
            doTestGroups();
        }

        private void doTestGroups()
        {
            txtResult.Clear();
            //string regexpGroupsSection = @"\[groups\]\r\n(?<Groups>(\s*\w+\s*=\s*\S*\r\n)*)(\s*\r\n)*(?<Comments>((#\s*\w+\s*=r?w?\r\n)*))";
            string regexpGroupsSection = @"\[groups\]\r\n(?<Groups>(\s*\w+\s*=\s*\S*\r\n)*)(\s*\r\n)*(?<Comments>((#\s*\w+\s*=r?w?\r\n)*))";
            if (txtRegExp.Text.Length > 0)
            {
                regexpGroupsSection = txtRegExp.Text;
            }
               
            Regex regGroupsSection = new Regex(regexpGroupsSection, RegexOptions.Multiline);
            Match GroupsSectionMatch = regGroupsSection.Match(txtSource.Text);
            string GroupsSectionText = GroupsSectionMatch.Value;

            Msg("============================");
            Msg(regexpGroupsSection);
            Msg("--------------------------");
            Msg(GroupsSectionText);
            GroupCollection gc = GroupsSectionMatch.Groups;
            Msg(gc.Count.ToString());
            Msg("--------------------------");

            string GroupsText=gc["Groups"].Value;
            Msg(GroupsText);
            Msg("--------------------------");
            
            string CommentsText=gc["Comments"].Value;
            Msg(CommentsText);
            Msg("--------------------------");

            //foreach (Group g in gc)
            //{
            //    Msg(g.Value);
            //    Msg("--------------------------");
            //}


            Msg("============================");
            string regexpGroup = @".*=.*\r\n";
            Msg(regexpGroup);
            Msg("--------------------------");
            Regex regGroups = new Regex(regexpGroup, RegexOptions.Multiline);

            MatchCollection matGroup = regGroups.Matches(GroupsText);
            Msg(matGroup.Count.ToString()); ;
            Msg("--------------------------");

            foreach (Match m in matGroup)
            {
                Msg(m.Value);
                Msg("--------------------------");
            }

            //MatchCollection mat = regGroupsSection.Matches(txtSource.Text);
            //Msg(mat.Count.ToString());
            //Msg("============================");

            //foreach (Match m in mat)
            //{
            //    Msg(m.Value);
            //    Msg("--------------------------");
            //}
        }
        private void Msg(string S)
        {
            txtResult.AppendText(S + "\n");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtSource.Text = (new System.IO.StreamReader(txtLog.Text)).ReadToEnd();
        }

        private void txtRegExp_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                doTest();
            }
        }
    }
}
